<?php

	/**
	 * 添加菜系前置操作
	 * 
	 */
	function cuisine_add_before($model){
		$model->city = $_POST['area2'];
		return $model;
	}

	/**
	 * 添加餐厅前置操作
	 * @param $model
	 */
	function outlet_add_before($model){
		
		if($model->id){
			$module = M("Outlet");
			$view   = $module->find($model->id);
			
			if(empty($view['solevar'])){
				$model->solevar = create_id();
			}
		}
	
		$ssxadd   = $model->ssxadd;
		$enssxadd = $model->enssxadd;
		$detail   = $model->detail;
		$endetail = $model->endetail;
		
		$model->city      = $_POST['city'];
		$model->area2     = $_POST['province'];
		$model->area4     = $_POST['district'];
		$model->address   = $ssxadd.$detail;
		$model->enaddress = $enssxadd.','.$endetail;
		$model->cuisine   = implode(',',$_POST['cuisine']);
		$model->week      = implode(',',$_POST['week']);
	
		if($model->title){
			$str = mb_substr($model->title,0,1,'utf-8');
	
			$model->alpha = D('Product')->is_chinese($str);
		}
		return $model;
	}
	
	/**
	 * 添加数据字典前置操作
	 * @param $model
	 */
	function dict_add_before($model){
	
		$value = $model->value;
		$pid   = $model->pid;
		$level = $model->level;
		
		if($pid){
			$level += 1;
		}
		$model->level     = $level;
		$model->value     = json_encode($value);
		$model->addtime   = time();
		return $model;
	}
	
	/**
	 * 操作数据字典后置操作
	 */
	function dict_add_after($table, $id){
		$model = M($table);
		$view  = $model->find($id);
		
		$map['key'] = $view['key'];
		$data = $model->where($map)->select();
		
		F("Dict_".$view['key'],$data);
	}
	
	/**
	 * 添加数据字典前置操作
	 * @param $model
	 */
	function dict_edit_before($model){
	
		$value = $model->value;
		$model->value = json_encode($value);
		
		return $model;
	}

	//餐厅后置操作-更新百度缓存
	function outlet_after_deal($table,$id,$action){
		
		$model = M("$table");
		$data  = $model->field("mapid,title,longitude,latitude,area,address,tags,tel,city")->find($id);
		$data["outletid"]    = $id;
		$data['geotable_id'] = C("MAP_OUTLET_ID");
		
		if($data["mapid"]){
			$data["id"]   = $data["mapid"];
			$json = mapStorge($data,"update");
		}else{
			$json = mapStorge($data,"create");
			$mapid= $json['id'] ;
			$model->save(array("id"=>$id,"mapid"=>$mapid));
		}
	
		if($json['status'] === 0){
			mapAll(true);
		}else{
			//$this->assign("jumpUrl",U("Outlet/edit","id=$id"));
			//$this->error("标记门店地址出错，请重新标记");
		}
	}

	/**
	 * 添加和修改节点后置操作
	 * 添加子节点
	 */
	function node_deal_after($table, $id, $action){
		
		$model = D("$table");
		$data  = $model->find($id);
		$level = $data['level'];
		$flag  = false;
		
		if($level == 1){
			
			load("@.hdmin");
			$module = $data['name'];
			$action = return_hdmin(true,$module);
			$flag   = true;
			$dengji = 2;
			$tid    = $id;
			$real   = 1;
		}elseif($level == 2){
			
			load("@.hdmin");
			$method = $data['name'];
			$module = $action->_req("module","trim");
			$action = return_hdmin(true,$module);
			$action = $action[$method]['child'];
			if(is_array($action)){
				$flag   = true;
				$dengji = 3;
				$tid    = $data['pid'];
				$real   = 2;
			}
		}
		
		if($flag){
			
			$func = $_POST['func'];
			$map  = array("pid"=>$id);
			$lang = getLang();

			if(is_array($func)){
				
				$actions = $model->where($map)->getField("name");
				foreach($action as $k=>$v){
					
					if(in_array($k, $func) && !in_array($k, $actions)){
						
						$row['tid']     = $tid;
						$row['pid']     = $id;
						$row['level']   = $dengji;
						$row['key']     = $data['key'];
						$row['permiss'] = $data['permiss'];
						$row['relation']= $real;
						$row['sort']    = $data['sort'];
						$row['url']     = $module."/".$k;
						$row['name']    = $k;
						$row['remark']  = $v[$lang];
						$row['title']   = $v[$lang];
						$row['addtime'] = time();
						$model->add($row);
					}
				}
			}
		}
		
	}
	
	/**
	 * 添加和修改角色后置操作
	 * 操作角色对应的节点
	 */
	function role_deal_after($table, $id) {
		
		$node  = $_POST['node'];
		$model = D("$table");
		$rows  = $model->find($id);
		
		if(is_array($node)){
			
			$module = M("AdminRoleAccess");
			$map    = array("role_id"=>$id);
			$module->where($map)->delete();
			
			foreach($node as $k=>$v){
				$data['node_id'] = $k;
				$data['role_id'] = $id;
				$data['pid']     = $rows['pid'];
				$data['sort']    = $rows['sort'];
				$data['level']   = 1;
				$module->add($data);
			}
		}
	}
	
	/**
	 * 修改角色后置操作
	 * 操作角色对应的菜单
	 */
	function role_menu_after($table, $id) {
		
		$node  = $_POST['node'];
		$model = D("$table");
		$rows  = $model->find($id);
		
		if(is_array($node)){
				
			$module = M("AdminMenuAccess");
			$map    = array("role_id"=>$id);
			$module->where($map)->delete();
				
			foreach($node as $k=>$v){
				$data['menu_id'] = $k;
				$data['role_id'] = $id;
				$data['pid']     = $rows['pid'];
				$data['sort']    = $rows['sort'];
				$data['level']   = 1;
				$module->add($data);
			}
		}
	}
	
	/**
	 * 修改用户前置操作
	 * 是否更新密码
	 */
	function admin_edit_before($model){
		
		$new = $model->password;
		$old = myencrypt(C('CONFUSE'));

		if($new == $old){
			$id  = $model->id;
			$row = $model->where("id=".$id)->getField("password");
			
			$model->password = $row;
		}
		return $model;
	}
	
	/**
	 * 添加商户-添加用户前置操作
	 * 添加角色、添加门店
	 */
	function admin_add_before($model){
		
		$comid   = $model->company;
		$company = M("Company")->find($comid);
		
		//添加角色
		$role['company'] = $company['id'];
		$role['name']    = $company['title'];
		$role['outlet']  = 1;
		$role['title']   = "管理员";
		$role['status']  = 2;
		$role['addid']   = $company['addid'];
		$role['addtime'] = time();
		$model->role = M("AdminRole")->add($role);
		
		//添加门店
		$outlet['company'] = $company['id'];
		$outlet['title']   = "总店";
		$outlet['mobile']  = $company['mobile'];
		$outlet['email']   = $company['email'];
		$outlet['addid']   = $company['addid'];
		$outlet['addtime'] = time();
		$outlet['status']  = 2;
		$model->outletid = M("Outlet")->add($outlet);
		$model->outlet   = $outlet['title'] ;
		
		$model->admin = 1;
		
		return $model;
	}
	
	/**
	 * 添加应用菜单前置操作
	 * json转义菜单配置
	 */
	function menu_deal_before($model, $action){
		
		$config = $_POST['config'];
		if(is_array($config)){
			$model->config = json_encode($config);
		}else{
			$model->config = "";
		}
		return $model;
	}
	
	/**
	 * 修改门店后置操作
	 * 修改用户对应的门店名称
	 */
	function Outlet_edit_after($table, $id) {
	
		$model = D("$table");
		$rows  = $model->find($id);
	
		$module = M("Admin");
		$map    = array("outletid"=>$id);
		$data   = array("outlet"  =>$rows['title']);
		$module->where($map)->save($data);
	}
	
	/**
	 * 修改商户后置操作
	 * 修改角色(admin_role)对应的商户名称
	 */
	function Company_edit_after($table, $id) {
	
		$model = D("$table");
		$rows  = $model->find($id);
	
		$module = M("Admin");
		$map    = array("company"=>$id);
		$data   = array("name"   =>$rows['title']);
		$module->where($map)->save($data);
	}